<?php
/**
 * Capa de abstraccion de productos.
 * Se usa para exportar datos al carrito, al tracker, o cualquier otra funcion
 * dependiente de un producto
 *
 */
class CartWS_Model_Tracker
{
	private $_config;
	
	public function __construct()
	{	
		$this->_config = Zend_Registry::get('config');
	}
	
    public function getOrderToTrack( $orderId )
    {
    	$orderModel = new CartWS_Model_SalesOrderHdr();
    	$addressModel = new CartWS_Model_Address();

    	$ordersData = $orderModel->getOrderData( $orderId );

        $exportData = array();
        foreach( $ordersData as $orderData ) { 
            /**
             * Datos basicos de la orden
             */
			$exportData[$orderId]['orderDate'] = Ext_Db_Date::now()->toString('yyyy-MM-dd HH:mm');
            $exportData[$orderId]['shippingMethod']['description'] = $orderData->shipping_method_id;
            $exportData[$orderId]['shippingMethod']['priority'] = $orderData->priority;
            $exportData[$orderId]['orderTotalAmount'] = $orderData->total_amount;
            $exportData[$orderId]['orderShippingPrice'] = $orderData->order_shipping_cost;
            $exportData[$orderId]['orderDiscount'] = 0;
        	/**
    		 * Propiedades de la orden
    		 */
            $orderProp = new CartWS_Model_SalesOrderHdrProperty();
            $properties = $orderProp->getAll($orderId);
            foreach ( $properties as $property ) {
				            	
            	$exportData[$orderId]['properties'][$property->property] = $property->value;
            }

            $exportData[$orderId]['properties']['shipToDetail']['value'] = 
            Zend_Json::encode( $addressModel->getAddressArray( $orderData->saddress_id ));
            $exportData[$orderId]['properties']['shipToDetail']['type'] = 'streetDetail';
            $exportData[$orderId]['properties']['billToDetail']['value'] = 
                Zend_Json::encode($addressModel->getAddressArray( $orderData->baddress_id ));
            $exportData[$orderId]['properties']['billToDetail']['type'] = 'streetDetail';

            /**
             * Datos del customer
             */
            $exportData[$orderId]['customer']['customerId'] = $orderData->customerId;
            $exportData[$orderId]['customer']['customerName'] = $orderProp->getProperty($orderId,  'customer_name');
            $exportData[$orderId]['customer']['customerEmail'] = $orderProp->getProperty($orderId, 'customer_email');
            $exportData[$orderId]['customer']['customerTel'] = $orderData->contact_phone;
            /**
             * Metodo de pago
             */
            $exportData[$orderId]['paymentMethod']['paymentMethodId'] =$this->_config->Payment->default->id;
            $exportData[$orderId]['paymentMethod']['description'] = $this->_config->Payment->default->description;
            $exportData[$orderId]['paymentMethod']['ticket'] = ( $orderData->ticket == 'T' );
            /**
             * Envio 
             */
            $dropPoint = ( $orderData->shipping_method_id == 
                $this->_config->Shipping->method->chileexpress) ? 0 : 1 ;
            $exportData[$orderId]['shipToAddress']['dropPoint'] = $dropPoint;            
            $exportData[$orderId]['shipToAddress']['addressId'] = $orderData->saddress_id;
            $address1 = $addressModel->getStringAddress( $orderData->saddress_id );
            $exportData[$orderId]['shipToAddress']['addressLine1'] = $address1;
            $exportData[$orderId]['shipToAddress']['addressLine2'] = '';
            $exportData[$orderId]['shipToAddress']['country'] = $orderData->szcountryn;
            $exportData[$orderId]['shipToAddress']['state'] = $orderData->szstaten;
            $exportData[$orderId]['shipToAddress']['county'] = $orderData->szcountyn;
            $exportData[$orderId]['shipToAddress']['city'] = $orderData->szcityn;
            $exportData[$orderId]['shipToAddress']['neighborhood'] = '';
            //
            $address2 = $addressModel->getStringAddress( $orderData->baddress_id );
            $dropPoint = ( $orderData->shipping_method_id ==
                $this->_config->Shipping->method->chileexpress) ? 0 : 1 ;
            $exportData[$orderId]['billToAddress']['dropPoint'] = $dropPoint;
            $exportData[$orderId]['billToAddress']['addressId'] = $orderData->baddress_id;
            $exportData[$orderId]['billToAddress']['addressLine1'] = $address2;
            $exportData[$orderId]['billToAddress']['addressLine2'] = '';
            $exportData[$orderId]['billToAddress']['country'] = $orderData->bzcountryn;
            $exportData[$orderId]['billToAddress']['state'] = $orderData->bzstaten;
            $exportData[$orderId]['billToAddress']['county'] = $orderData->bzcountyn;
            $exportData[$orderId]['billToAddress']['city'] = $orderData->bzcityn;
            $exportData[$orderId]['billToAddress']['neighborhood'] = '';
			$exportData[$orderId]['lines'] = $this->_getOrderLineToExport( $orderId );
        }
        return $exportData;        
    }             
    private function _getOrderLineToExport( $orderId )
    {
    	$lineModel = new CartWS_Model_SalesOrderLine();
		$lines = $lineModel->getLinesFromOrder( $orderId );
        /**
         * Generamos las propiedades de Cada item de la orden
         */
        foreach( $lines as $line ){
        	$description['title'] = $line['product_description'];
			$description['authors'] = '';
	
        	$sellingPrice = $line['unit_selling_price'] + (
        		($line['unit_selling_price'] * $this->_config->Payment->iva->percentage )/100.00
        	);
        	
			$exportData[$line['sales_line_id']]['productId'] = (int) $line['product_id'];
			$exportData[$line['sales_line_id']]['productDescription'] = Zend_Json::encode($description);
			$exportData[$line['sales_line_id']]['productDetail'] = $line['product_detail'];
			$exportData[$line['sales_line_id']]['productType'] = $line['product_type'];
			$exportData[$line['sales_line_id']]['orderedQuantity'] = $line['ordered_quantity'];
			$exportData[$line['sales_line_id']]['productSellingPrice'] = $sellingPrice;
			$exportData[$line['sales_line_id']]['lineShippingPrice'] = $line['unit_shipping_price'];
			$exportData[$line['sales_line_id']]['lineDiscount'] = 0;
			$exportData[$line['sales_line_id']]['tax'] = $this->_config->Payment->iva->percentage;
			$exportData[$line['sales_line_id']]['totalAmount'] = $line['unit_final_price'];
		}
		return $exportData;
    }
}
